package com.example.tableapplication.dao;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


public class SOAPUtil {
    public static Object doTransport(final String wsdUrl, final String webMethod,String json) {
        String nameSpace = "http://service";//一般都是默认的
        SoapObject soapObject = new SoapObject(nameSpace, webMethod);
//        if(param!=null){
//            for(Map.Entry<String, Object> entry: param.entrySet())
//            {
//                System.out.println("Key: "+ entry.getKey()+ " Value: "+entry.getValue());
//                soapObject.addProperty(entry.getKey().toString(),entry.getValue().toString());
//            }
//        }

        soapObject.addProperty("json",json);
        System.out.println();
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        soapSerializationEnvelope.bodyIn = soapObject;
        soapSerializationEnvelope.dotNet = false;
        soapSerializationEnvelope.setOutputSoapObject(soapObject);
        HttpTransportSE httpTransportSE = new HttpTransportSE(wsdUrl);
        String SOAP_ACTION = "http://service" + webMethod;
        //输出soapAction
        System.out.println(SOAP_ACTION);
        try {
            httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);
            System.out.println("调用结束");
            //输出响应
            System.out.println(soapSerializationEnvelope.getResponse());
            if (soapSerializationEnvelope.getResponse() != null) {
                Object result = (Object) soapSerializationEnvelope.getResponse();
                //输出结果
//                for (int i = 0; i < result.getPropertyCount(); i++) {
////                    System.out.println("result [" + i + "] = "+ result.getProperty(i).toString());
////                }
                System.out.println(result.toString());

                return result;
            }
        } catch (IOException e) {
            System.out.println("IOException");
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
        return null;
    }
}
